package com.jnsw.testkotlin.updateapp;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.widget.Toast;

import com.allenliu.versionchecklib.AVersionService;

public class MyService extends AVersionService {
    private String downladurl = "www.baidu.com";
    private String title = "发现新版本";
    private String msg = "新版本特性.....";

    /**
     * 接收返回信息 apk下载地址 新版本号 更新信息
     * @param service
     * @param response
     */
    @Override
    public void onResponses(AVersionService service, String response) {
        //参数从response 中读取 传入以下方法提示更新app
    System.out.println("版本信息 -----"+getVersionCode()+"-----------"+getVersionName());
        Toast.makeText(getApplicationContext(),"版本信息 -----"+getVersionCode()+"-----------"+getVersionName(),Toast.LENGTH_SHORT).show();
//    HttpParams params = new HttpParams();
//        params.put("type","android");

        System.out.println(response);

        /*
        根据返回的版本号对比 查看是否需要下载更新
         */

        service.showVersionDialog(downladurl, title, msg); // 打开DialogActivity 提示更新信息
    }

    public int getVersionCode() {
        try {
            PackageInfo pinfo = getApplicationContext().getPackageManager().getPackageInfo(
                    "com.jnsw.testkotlin", PackageManager.GET_CONFIGURATIONS);
            return pinfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
        }
        return 0;
    }

    public String getVersionName() {
        try {
            PackageInfo pinfo = getApplicationContext().getPackageManager().getPackageInfo(
                    "com.jnsw.testkotlin", PackageManager.GET_CONFIGURATIONS);
            return pinfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
        }
        return null;
    }
}
